import queue
import threading

import uiautomator2 as u2


# 待实现...
class AutoReplyThreadPool:
    def __init__(self, device_and_app_list):
        self.__device_and_app_list = device_and_app_list  # [(), ()]
        self.__thread_pool = queue.Queue(maxsize=10)

    def __initialize_thread_pool(self):
        pass

    def get_thread_in_pool(self):
        return self.__thread_pool.get()


class MessageThreadPool:
    def __init__(self, app):
        self.__app = app
        self.__message_queue = queue.Queue(maxsize=5)
        self.__ignore_message = ['', '[AI]回复: ']
        self.running = False

    def start_running(self):
        self.running = True

    def stop_running(self):
        self.running = False

    def get_new_message(self):
        if not self.__message_queue.empty():
            return self.__message_queue.get()
        return None

    def get_message_queue_size(self):
        return self.__message_queue.qsize()

    def start_message_listener(self, window, el_id):
        call_thread_method = '_MessageThreadPool__' + self.__app.lower() + '_message_listener'
        if call_thread_method in MessageThreadPool.__dict__:
            thread_method = getattr(self, call_thread_method)
            if callable(thread_method):
                thread_method(window, el_id)

    def __wechat_message_listener(self, window, el_id):
        def message_listener():
            while self.running:
                try:
                    el = window(className=el_id)[-1]
                    if el.info['bounds']['right'] <= 1048 and el.info['bounds']['bottom'] <= 2559:
                        if el.get_text() not in self.__ignore_message:
                            self.__ignore_message[0] = el.get_text()
                            self.__message_queue.put(el.get_text())
                except AssertionError:
                    continue
                except u2.exceptions.UiObjectNotFoundError:
                    continue

        threading.Thread(target=message_listener, daemon=True).start()

    def __other_message_listener(self):
        pass


if __name__ == '__main__':
    a = '[AI]回复: B+树是一种树数据结构，通常用于数据库和操作系统的文件系统中'
    print('[AI]回复: ' not in a)
    print(not True)
    list1 = ['1', '2', '3']
    list2 = ['4', '5', '6']
    print(list(zip(list1, list2))[0][1])
